package com.qiyang.stream;

import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created on 2018/8/6.
 *
 * @author sehuaqi.
 * 描述：
 */
public class StreamDemo4 {


    public static void main(String args[]) {
        //执行函数
        String str = "My Name is 007";
        //使用并行流
        str.chars().parallel().forEach(i -> System.out.println((char) i));
        System.out.println("=================");
        //使用forEachOrdered 保证顺序
        str.chars().parallel().forEachOrdered(i -> System.out.println((char) i));

        //collect
        List<String> list = Stream.of(str.split(" ")).collect(Collectors.toList());
        System.out.println(list);

        //使用reduce 拼接字符串
        Optional<String> reduce = Stream.of(str.split(" ")).reduce((s1, s2) -> s1 + "|" + s2);
        System.out.println(reduce);

        //计算所有单词总长度
        Integer length = Stream.of(str.split(" ")).map(s -> s.length()).reduce(0, (s1, s2) -> s1 + s2);
        System.out.println(length);

        //max
        Optional<String> max = Stream.of(str.split(" ")).max((s1, s2) -> s1.length() - s2.length());

        System.out.println(max);

        //短路操作
        OptionalInt first = new Random().ints().findFirst();
        OptionalInt any = new Random().ints().findAny();

        System.out.println(first);
        System.out.println(any);
    }

}
